<?php

require_once 'IOperations.php';
require_once 'Connexion.class.php';
require_once '../modele/Activite.class.php';
require_once '../modele/Projet.class.php';

class AccesDonneesActivite implements IOperations
{
    private $activite;
    private $projet;
    private $utilisateur;
    
    public function __construct ($activite, $projet, $utilisateur)
    {
	$this->activite = $activite;
	$this->projet = $projet;
	$this->utilisateur = $utilisateur;
	
	try
	{
	    $this->initConnexion = new Connexion();
	    $this->connexionBDD = $this->initConnexion->connexionBDD();
	}
	catch (PDOException $erreur)
	{
	    echo 'Erreur, classe : ' . __CLASS__ .  ', méthode : ' . __METHOD__;
	}
    }
    
    public function creation()
    {
	$usrID = $this->utilisateur->getUsrID();
	$projetDateCreation = date('d/m/Y H:i:s');
	$prjNom = 'Défaut';
	$prjCommentaire = 'Défaut';
	
	if ($this->projet->getPrjID() == 'aucun')
	{
	    echo 'id aucun';
	    $requetePreparee = $this->connexionBDD->prepare("INSERT INTO s_gestion_projet.v_infos_t_projet_prj
			    (usr_id, prj_date_creation, prj_nom, prj_commentaire)
			    VALUES (?, ?, ?, ?)");
	
	    $requetePreparee->bindParam(1,$usrID);
	    $requetePreparee->bindParam(2,$projetDateCreation);
	    $requetePreparee->bindParam(3,$prjNom);
	    $requetePreparee->bindParam(4,$prjCommentaire);
	
	    $execRequeteSQL = $requetePreparee->execute();
	    
	    $requetePreparee = $this->connexionBDD->prepare("SELECT max(prj_id) AS prj_id FROM s_gestion_projet.v_infos_t_projet_prj");
	
	    $execRequeteSQL = $requetePreparee->execute();
	    $resultatSQL = $requetePreparee->fetchAll(PDO::FETCH_ASSOC);
	    $this->projet->setPrjID($resultatSQL[0]['prj_id']);
	}
	
	$id = $this->projet->getPrjID();
	$label = $this->activite->getActvLabel();
	$priorite = $this->activite->getActvPriorite();
	$dateCreation = $this->activite->getActvDateCreation();
	$dateEcheance = $this->activite->getActvDateEcheance();
	$etat = $this->activite->getActvEtat();
	
	$requetePreparee = $this->connexionBDD->prepare("INSERT INTO s_gestion_projet.v_infos_t_activite_actv
			    (prj_id, actv_label, actv_priorite, actv_date_creation, actv_date_echeance, actv_etat)
			    VALUES (?, ?, ?, ?, ?, ?)");
	
	$requetePreparee->bindParam(1,$id);
	$requetePreparee->bindParam(2,$label);
	$requetePreparee->bindParam(3,$priorite);
	$requetePreparee->bindParam(4,$dateCreation);
	$requetePreparee->bindParam(5,$dateEcheance);
	$requetePreparee->bindParam(6,$etat);
	
	$execRequeteSQL = $requetePreparee->execute();
	
	return $execRequeteSQL;
    }
    
    public function selection()
    {	
	$id = $this->activite->getActvID();
	
	$requetePreparee = $this->connexionBDD->prepare("SELECT * FROM s_gestion_projet.v_infos_t_activite_actv
							WHERE actv_id = ?");
	
	$requetePreparee->bindParam(1,$id);
	
	$execRequeteSQL = $requetePreparee->execute();
	$resultatSQL = $requetePreparee->fetchAll(PDO::FETCH_ASSOC);	
	$tableauActiviteSelection = array();
    
	foreach ($resultatSQL as $ligne)
	{   
	    $tableauActiviteSelection['actv_id'] = $ligne['actv_id'];
	    $tableauActiviteSelection['prj_id'] = $ligne['prj_id'];
	    $tableauActiviteSelection['actv_label'] = $ligne['actv_label'];
	    $tableauActiviteSelection['actv_priorite'] = $ligne['actv_priorite'];
	    $tableauActiviteSelection['actv_date_creation'] = $ligne['actv_date_creation'];
	    $tableauActiviteSelection['actv_date_echeance'] = $ligne['actv_date_echeance'];
	    $tableauActiviteSelection['actv_etat'] = $ligne['actv_etat'];
	}
	
	return $tableauActiviteSelection;
    }
    
    public function selectionToutesActivites()
    {	
	$requetePreparee = $this->connexionBDD->prepare("SELECT * FROM s_gestion_projet.v_infos_t_activite_actv");
	
	$execRequeteSQL = $requetePreparee->execute();
	$resultatSQL = $requetePreparee->fetchAll(PDO::FETCH_ASSOC);	
	
	return $resultatSQL;
    }
    
    public function selectionGrouperParProjets()
    {
	$id = $this->projet->getPrjID();
	
	$requetePreparee = $this->connexionBDD->prepare("SELECT * FROM s_gestion_projet.v_infos_t_activite_actv
							WHERE prj_id = ?");
	
	$requetePreparee->bindParam(1,$id);
	
	$execRequeteSQL = $requetePreparee->execute();
	$resultatSQL = $requetePreparee->fetchAll(PDO::FETCH_ASSOC);	
	$tableauActiviteSelection = array(array());
    
	$i = 0;
	foreach ($resultatSQL as $ligne)
	{   
	    $tableauActiviteSelection[$i]['actv_id'] = $ligne['actv_id'];
	    $tableauActiviteSelection[$i]['prj_id'] = $ligne['prj_id'];
	    $tableauActiviteSelection[$i]['actv_label'] = $ligne['actv_label'];
	    $tableauActiviteSelection[$i]['actv_priorite'] = $ligne['actv_priorite'];
	    $tableauActiviteSelection[$i]['actv_date_creation'] = $ligne['actv_date_creation'];
	    $tableauActiviteSelection[$i]['actv_date_echeance'] = $ligne['actv_date_echeance'];
	    $tableauActiviteSelection[$i]['actv_etat'] = $ligne['actv_etat'];
	
	    $i++;
	}
	
	return $tableauActiviteSelection;
    }
    
    public function selectionActivitesProjets ()
    {
	$id = $this->utilisateur->getUsrID();
	
	$requetePreparee = $this->connexionBDD->prepare("SELECT actv_id,
								actv_label,
								to_char(actv_date_creation,'DD/MM/YYYY à HH24:MI:SS') AS actv_date_creation,
								to_char(actv_date_echeance,'DD/MM/YYYY à HH24:MI:SS') AS actv_date_echeance,
								actv_etat, 
								actv_priorite, prj_nom, usr_id
							FROM s_gestion_projet.v_infos_t_projet_activite_prj
							WHERE usr_id = ? ORDER BY actv_id");
	
	$requetePreparee->bindParam(1,$id);
	
	$execRequeteSQL = $requetePreparee->execute();
	$resultatSQL = $requetePreparee->fetchAll(PDO::FETCH_ASSOC);	
	
	return $resultatSQL;
    }
    
    public function selectionActivitesProjetsTrierEcheance ($intervalleDateDebut = '', $intervalleDateFin = '', $listeProjets = '')
    {
	$id = $this->utilisateur->getUsrID();
	
	$triDate = 'ASC';
	
	$entreIntervalleDate = '';
	if ($intervalleDateDebut != '' && $intervalleDateFin != '')
	{
	    $entreIntervalleDate = ' AND echeance BETWEEN \''.$intervalleDateDebut.'\' AND \''.$intervalleDateFin.'\'';
	    echo $entreIntervalleDate;
	}
	
	$formeRequeteProjet = '';
	if (!empty($listeProjets))
	{
	    echo 'Liste prj non vide';
	    $formeRequeteProjet = 'AND prj_id IN (';
	    for ($i = 0, $taille = count($listeProjets); $i < $taille; $i++)
	    {
		if ($i == ($taille - 1))
		{
		    $formeRequeteProjet .= $listeProjets[$i] . ')';
		}
		else
		{
		    $formeRequeteProjet .= $listeProjets[$i] . ',';
		}
	    }
	}
	
	$requetePreparee = $this->connexionBDD->prepare("SELECT actv_id,
								actv_label,
								to_char(actv_date_creation,'DD-MM-YYYY HH24:MI:SS') AS actv_date_creation,
								to_char(actv_date_echeance,'DD-MM-YYYY HH24:MI:SS') AS actv_date_echeance,
								echeance,
								actv_etat, 
								actv_priorite, prj_id, prj_nom, usr_id
							FROM s_gestion_projet.v_infos_t_projet_activite_prj_intervalle
							WHERE usr_id = ? $entreIntervalleDate $formeRequeteProjet ORDER BY echeance $triDate");
	
	$requetePreparee->bindParam(1,$id);
	
	$execRequeteSQL = $requetePreparee->execute();
	$resultatSQL = $requetePreparee->fetchAll(PDO::FETCH_ASSOC);	
	$tableauActiviteSelection = array();
	
	return $resultatSQL;
    }
    
    public function suppression()
    {
	$id = $this->activite->getActvID();
	var_dump($id);
				
	$requetePreparee = $this->connexionBDD->prepare("DELETE FROM s_gestion_projet.v_infos_t_activite_actv
							WHERE actv_id = ?");
	
	$requetePreparee->bindParam(1,$id);
	
	$execRequeteSQL = $requetePreparee->execute();
	
	return $execRequeteSQL;
    }
    
    public function miseAJour()
    {
	$label = $this->activite->getActvLabel();
	$priorite = $this->activite->getActvPriorite();
	$dateCreation = $this->activite->getActvDateCreation();
	$dateEcheance = $this->activite->getActvDateEcheance();
	$etat = $this->activite->getActvEtat();
	$id = $this->activite->getActvID();
	
	$requetePreparee = $this->connexionBDD->prepare("UPDATE s_gestion_projet.v_infos_t_activite_actv
				SET actv_label = ?,
				actv_priorite = ?,
				actv_date_creation = ?,
				actv_date_echeance = ?,
				actv_etat = ?
				WHERE actv_id = ?");
	
	$requetePreparee->bindParam(1,$label);
	$requetePreparee->bindParam(2,$priorite);
	$requetePreparee->bindParam(3,$dateCreation);
	$requetePreparee->bindParam(4,$dateEcheance);
	$requetePreparee->bindParam(5,$etat);
	$requetePreparee->bindParam(6,$id);
				
	$execRequeteSQL = $requetePreparee->execute();
	
	return $execRequeteSQL;
    }
}
?>

